package cn.wolfcode.Controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.mapper.ActivityMapper;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;
    @Autowired
    private ActivityMapper activityMapper;


    @RequirePermission(name = "活动页面页面",experssion = "activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("flag", 0);
        return "activity/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "活动页面页面",experssion = "activity:list")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            activityService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/input")
    @RequirePermission(name = "活动投票页面页面",experssion = "activity:input")
    public String input(Long id, Model model){
        Set<Long> list = activityMapper.selectByItemId(UserContext.getCurrentEmployee().getId());
        System.out.println(list);
        List<Long> eid = new ArrayList<>();
        for (Long l:list){
            eid.add(activityMapper.selectByItemId2(l));
        }
        System.out.println(eid);
        System.out.println(id);
        for (Long l:eid){
            if (l==id){
                model.addAttribute("flag", 1);
                System.out.println(1);
                return "redirect:/activity/list";
            }
        }
{
            model.addAttribute("flag", 0);
            System.out.println(0);
            Activity activity = activityService.selectById(id);
            model.addAttribute("activity",activity);
            return "activity/input";
        }


    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "活动页面页面",experssion = "activity:list")
    public String saveOrUpdate(Long optionsRadios,Long activeId,Model model){
        activityService.insertRelation(UserContext.getCurrentEmployee().getId(),optionsRadios,new Date());
        activityItemService.updateNum(optionsRadios);
        List<ActivityItem> items = activityItemService.selectByAId(activeId);
        model.addAttribute("items",items);
        int num = activityService.selectNum(activeId);
        model.addAttribute("total",num);
        return "activity/total";
    }
}
